package com.linxi.handy.component.control;

import com.linxi.handy.Application;
import com.linxi.handy.common.Constants;
import com.linxi.handy.component.listener.DatabaseList;
import com.linxi.handy.component.menu.TopMenu;
import com.linxi.handy.component.module.ExecSqlResultDisplay;
import com.linxi.handy.component.tab.TabbedPane;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

/**
 * 入口
 *
 * @author user
 */
public class HomeControl {
    public static JFrame base;
    public static TabbedPane ft;
    public static ExecSqlResultDisplay fdi;
    public static DatabaseList fdb;
    static int fx = 1200, fy = 800;
    static TopMenu fm;

    public HomeControl() {
        base = new JFrame("Handy");
        fm = new TopMenu();
        fdb = new DatabaseList();
        ft = new TabbedPane();
        fdi = new ExecSqlResultDisplay();

        base.add(fm);
        base.add(fdb);
        base.add(ft);
        base.add(fdi);

        base.setLayout(null);
        base.addComponentListener(new sizeProc());

        Image image = new ImageIcon(getClass().getResource(Constants.LOGO)).getImage();
        base.setIconImage(image);

        base.setSize(fx, fy);
        //将窗体设置在屏幕中间
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension size = base.getSize();
        int x = (screenSize.width - size.width) / 2;
        int y = (screenSize.height - size.height) / 2;
        base.setLocation(x, y);
        base.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        base.setVisible(true);

        //构建完毕，关闭登录窗口
        Application.login.dispose();
    }

    class sizeProc implements ComponentListener {
        @Override
        public void componentHidden(ComponentEvent arg0) {

        }

        @Override
        public void componentMoved(ComponentEvent arg0) {

        }

        @Override
        public void componentResized(ComponentEvent arg0) {
            Dimension size = base.getSize();
            int x = size.width / 4;
            int y = size.height / 4;
            fm.setBounds(0, 0, size.width, 30);
            fdb.setBounds(0, 30, x, size.height - 70);
            ft.setBounds(x, 30, 3 * x - 20, 3 * y);
            fdi.setBounds(x, 3 * y + 30, 3 * x - 20, y - 70);
            fm.updateUI();
            fdb.updateUI();
            ft.updateUI();
            fdi.updateUI();
        }

        @Override
        public void componentShown(ComponentEvent arg0) {

        }
    }
}
